//
//  WNInboxVC.swift
//  WalkNovel
//
//  Created by Kevin on 2023/8/31.
//

import UIKit

class WNInboxVC: WNBaseVC {
    
    lazy var inboxTabView: WNTableView = {
        let tab = WNTableView.init(frame: CGRect.zero, style: .plain)
        tab.delegate = self
        tab.dataSource = self
        tab.register(SystemInboxCell.self, forCellReuseIdentifier: SystemInboxCell.identifier)
        tab.register(BookInboxCell.self, forCellReuseIdentifier: BookInboxCell.identifier)
        tab.ly_emptyView = WNEmptyView.wn_loadStyle(descStr: .inbox, imageStr: .inbox, tapBlock: {
            [weak self] in
            tab.mj_header?.beginRefreshing()
        })
        tab.isHidden = true
        return tab
    }()
    
    var msgArr = [MessageModel]()
        
    override func viewDidLoad() {
        super.viewDidLoad()
        handleNoti()
    }
    
    override func viewConfig() {
        view.addSubview(inboxTabView)
        inboxTabView.snp.makeConstraints { (make) in
            make.top.equalTo(NaviTotalHeight)
            make.left.bottom.right.equalTo(0)
        }
    }
    
    override func dataConfig() {
        pageName = "收件匣"
        navView.titleName = "收件匣"
        listSettingRefresh()
        inboxListRequest()
    }
}

extension WNInboxVC {

    func listSettingRefresh() {
        inboxTabView.mj_header = wn_refreshHeader(completion: { [weak self] in
            self?.page = 1
            self?.hasPage = false
            self?.inboxListRequest()
        })
        
        inboxTabView.mj_footer = wn_refreshFooter(completion: { [weak self] in
            if self?.page == 1 && (self?.msgArr.count ?? 0) == 0 {
                self?.inboxTabView.mj_footer?.endRefreshing()
                return
            }
            if self?.hasPage == true {
                self?.page += 1
                self?.inboxListRequest()
            }else {
                self?.inboxTabView.mj_footer?.endRefreshingWithNoMoreData()
            }
        })
    }
    
    /// 系统消息请求
    func inboxListRequest() {
        provider_message.rx.request(.messageList(page))
            .asObservable().mapModel(BaseModel<[MessageModel]>.self)
            .subscribe(onNext: { [weak self] (model) in
                self?.inboxTabView.isHidden = false
                self?.inboxTabView.ly_emptyView.imageStr = EmptyImage.inbox.rawValue
                self?.inboxTabView.ly_emptyView.detailStr = EmptyDesc.inbox.rawValue
                self?.inboxTabView.ly_emptyView.btnTitleStr = ""
                guard model.code == 0 else {
                    if (self?.isFirst ?? true) {
                        self?.inboxTabView.ly_emptyView.imageStr = EmptyImage.serverBad.rawValue
                        self?.inboxTabView.ly_emptyView.detailStr = EmptyDesc.serverBad.rawValue
                        self?.inboxTabView.ly_emptyView.btnTitleStr = "點擊刷新"
                    }else {
                        self?.wn_toast(model.msg ?? "")
                    }
                    self?.inboxTabView.mj_header?.endRefreshing()
                    self?.inboxTabView.mj_footer?.endRefreshing()
                    self?.isFirst = false
                    return
                }
                if self?.page == 1 {
                    self?.msgArr.removeAll()
                    self?.inboxTabView.mj_footer?.resetNoMoreData()
                }
                if let arr = model.data {
                    self?.hasPage = arr.count > 9
                    self?.msgArr += arr
                }else {
                    self?.inboxTabView.mj_footer?.endRefreshingWithNoMoreData()
                }
                self?.inboxTabView.reloadData()
                self?.inboxTabView.mj_header?.endRefreshing()
                self?.inboxTabView.mj_footer?.endRefreshing()
                self?.isFirst = false
            }, onError: { [weak self] (error) in
                self?.wn_toast("檢查網路連線")
                self?.inboxTabView.ly_emptyView.imageStr = EmptyImage.netWork.rawValue
                self?.inboxTabView.ly_emptyView.detailStr = EmptyDesc.netWork.rawValue
                self?.inboxTabView.ly_emptyView.btnTitleStr = "點擊刷新"
                self?.inboxTabView.mj_header?.endRefreshing()
                self?.inboxTabView.mj_footer?.endRefreshing()
                self?.inboxTabView.isHidden = false
            }).disposed(by: rx.disposeBag)
    }
}

//MARK: - TableViewDelegate, UITableViewDataSource
extension WNInboxVC: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return msgArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if msgArr[indexPath.item].type == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: SystemInboxCell.identifier, for: indexPath) as! SystemInboxCell
            cell.model = msgArr[indexPath.item]
            return cell
        }else {
            let cell = tableView.dequeueReusableCell(withIdentifier: BookInboxCell.identifier, for: indexPath) as! BookInboxCell
            cell.model = msgArr[indexPath.item]
            return cell
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if msgArr[indexPath.item].type == 2 {
            wn_pushBookVC(msgArr[indexPath.item].bookId ?? "")
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return msgArr[indexPath.item].allH
    }
}
